//
//  WD_TableViewTimeLine.m
//  BUDING
//
//  Created by 景强 on 16/3/31.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_TableViewTimeLine.h"
#import "WD_CollectionViewCellTimeLine.h"
#import "WD_imageModel.h"
#import "DCPicScrollView.h"
#import "WD_ShowImageViewController.h"
@class  WD_TimeLineViewController;
@interface WD_TableViewTimeLine()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *pictureSource;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIButton *buttont;

@end





@implementation WD_TableViewTimeLine

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        
        self.imageViewAvatar = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.labelNickName = [[UILabel alloc] initWithFrame:CGRectZero];
        self.labelNickName.font = [UIFont systemFontOfSize:15 weight:0.7];
        self.labelContent = [[UILabel alloc] initWithFrame:CGRectZero];
        self.labelContent.numberOfLines = 3;
        self.labelContent.font = [UIFont systemFontOfSize:13];
        self.buttont = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttont.frame = CGRectZero;
        [self.contentView addSubview:self.imageViewAvatar];
        [self.contentView addSubview:self.labelNickName];
        [self.contentView addSubview:self.labelContent];
        [self.contentView addSubview:self.buttont];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(100, 100);
        
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[WD_CollectionViewCellTimeLine class] forCellWithReuseIdentifier:@"WD_CollectionViewCellTimeLine"];
        [self.contentView addSubview:self.collectionView];
        
    }
    
    return self;
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.imageViewAvatar.frame = CGRectMake(10, 10, 90, 90);
    
    self.imageViewAvatar.layer.masksToBounds = YES;
    self.imageViewAvatar.layer.cornerRadius = self.imageViewAvatar.frame.size.height / 2;
    
    self.buttont.frame = CGRectMake(10, 10, 90, 90);
    //    [self.buttont addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpOutside];
    
    
    self.labelNickName.frame = CGRectMake(110, 20, self.frame.size.width - 30, 50);
    self.labelContent.frame = CGRectMake(20, 90, self.frame.size.width - 30, 80);
    
    self.collectionView.frame = CGRectMake(0, 160, self.frame.size.width, self.frame.size.height - 150);
}

- (void)setModel:(WD_OwnerModel *)model {
    
    _model = model;
    
    //    if (![_labelNickName.text isEqualToString:model.nickname]) {
    
    self.labelNickName.text = model.nickname;
    //    }
    
    
    
}

- (void)setModelRelate:(WD_RelatedModel *)modelRelate {
    
    _modelRelate = modelRelate;
    _pictureSource = [NSMutableArray arrayWithCapacity:0];
    
    for (NSDictionary *dic in _modelRelate.images) {
        WD_imageModel *imageModel = [[WD_imageModel alloc] init];
        [imageModel setValuesForKeysWithDictionary:dic];
        [_pictureSource addObject:imageModel];
    }
    _labelContent.text = modelRelate.content;
    [_collectionView reloadData];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WD_CollectionViewCellTimeLine *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WD_CollectionViewCellTimeLine" forIndexPath:indexPath];
    cell.imageModel = self.pictureSource[indexPath.item];
    //    WD_imageModel *model = self.cellArray[indexPath.item];
    //
    //    NSString *str  = model.url;
    //    NSLog(@"%@", str);
    return cell;
    
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.pictureSource.count;
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    
    
    //    WD_ShowImageViewController *showVC = [[WD_ShowImageViewController alloc] init];
    for (int i = 0; i < self.cellArray.count; i++) {
        WD_imageModel *model = self.cellArray[i];
        [array addObject:model.url];
        
    }
    self.blockRow(indexPath.item);
}



//- (void)buttonAction:(UIButton *)sender {
//
//
//
//
//
////    _cellArray = cellArray;
////
////    [self.collectionView reloadData];
//}


- (void)setCellArray:(NSMutableArray *)cellArray {
    
    _cellArray = cellArray;
    
    [_collectionView reloadData];
    
}












/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
